<?php

namespace App\Controller\Home;

use App\Controller\Example\UserExample;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Result\Result;
use Core\Utils\Upload\aliOssUpload;

class Upload
{
    public function upfile($data){
        $assemble = Common::assemble(new UserExample(), $data);
        $type = $data["type"];
        $file = $_FILES['file'];
        if(!isset($file)){
            Result::error(ErrorEnum::PLEASEUPLOADTHEFILE[$assemble->language]);
        }
        $imgType = ['png', 'jpg', 'jpeg', 'bmp', 'gif'];
        $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
        if (!in_array($ext, $imgType)) {
            Result::error(ErrorEnum::FILETYPEERROR[$assemble->language]);
        }
        $file_size = number_format(($file['size'] / 1024 / 1024), 2); //MB
        if ($file_size > 2) {
            Result::error(ErrorEnum::THEFILEEXCEEDSTHELIMITSIZEOF2M[$assemble->language]);
        }
        switch ($type) {
            case 1://收款二维码
                $saveName = 'receiving-code/';
                break;
            case 2://头像
                $saveName = 'header-img/';
                break;
            case 3://申诉
                $saveName = 'appeal-img/';
                break;
            default:
                Result::error(ErrorEnum::ILLEGALBINDINGTYPE[$assemble->language]);
        }
        $saveName .= date('YmdHis') . rand(10001, 99999) . '.' . $ext;
        $aliOssUpload = new aliOssUpload();
        $imgInfo = $aliOssUpload->upload($file['tmp_name'], $saveName);
        Result::success($imgInfo["oss-request-url"]);
    }
}